/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.tmall.batch.TmallSyncOrder
   Module Description   :

   Date Created      : 2011/11/10
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.tmall.batch;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;

import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.ec.tmall.service.TmallService;

/**
 * @author cut7man
 * 
 */
public class TmallSyncOrder {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private static SessionFactory sessionFactory;
	protected static ClassPathXmlApplicationContext ctx = null;
	private TmallService tmallService;

	public static void main(String args[]) {
		String startDateStr = null;
		String endDateStr = null;
		Date startDate = null;
		Date endDate = null;
		if (args != null && args.length >= 2) {
			startDateStr = args[0];
			endDateStr = args[1];
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
			try {
				startDate = sdf.parse(startDateStr);
				endDate = sdf.parse(endDateStr);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Integer cpage = null;
		if (args != null && args.length > 2) {
			cpage = Integer.valueOf(args[2]);
		}
		ctx = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml", "applicationContext-*.xml" });
		String username = System.getenv("USERNAME");
		if (StringUtils.isBlank(username)) {
			username = System.getenv("USER");
		}
		if (StringUtils.isBlank(username)) {
			username = "test00";
		}
		if (ctx != null && ctx.getBean("sessionFactory") != null) {
			sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
		}
		User tester = new User(username);
		SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(tester, "password"));

		TmallSyncOrder tmallSyncOrder = new TmallSyncOrder();
		tmallSyncOrder.execute(startDate, endDate, cpage);
	}

	public TmallSyncOrder() {
		tmallService = ctx.getBean(TmallService.class);
	}

	public void execute(Date cstartDate, Date cendDate, Integer cpage) {
		int page = 5;
		int pageSize = 20;
		Date now = new Date();
		Date startDate = DateUtils.addMinutes(now, -72);
		Date endDate = DateUtils.addMinutes(now, -60);
		if (cstartDate != null) {
			startDate = cstartDate;
		}
		if (cendDate != null) {
			endDate = cendDate;
		}

		Integer totalResult = tmallService.tradesSoldIncrementGet(startDate, endDate, null, null, 1, pageSize);
		page = totalResult / pageSize + 1;
		if (cpage != null) {
			page = cpage;
		}

		for (int i = 2; i <= page; i++) {
			System.out.println("start of process page:" + i);
			try {
				tmallService.tradesSoldIncrementGet(startDate, endDate, null, null, i, pageSize);
			} catch (Exception e) {
				e.printStackTrace();
				logger.warn(e.getMessage());
			}
			System.out.println("end of process page:" + i);
		}
	}
}
